package com.fxkj.sthjt.db.controller;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxkj.sthjt.db.entity.DChannel;
import com.fxkj.sthjt.db.entity.JcChannelExt;
import com.fxkj.sthjt.db.entity.NewChannel;
import com.fxkj.sthjt.db.mapper.DChannelMapper;
import com.fxkj.sthjt.db.mapper.JcChannelExtMapper;
import com.fxkj.sthjt.db.mapper.NewChannelMapper;

/**
 * <p>
 * 前端控制器
 * </p>
 * http://localhost:7777/sthjt/d-channel/channel_cnt
 * 
 * @author 冯晓东
 * @since 2021-09-12
 */
@RestController
@RequestMapping("/d-channel")
public class DChannelController {

	@Autowired
	private DChannelMapper dChannelMapper;
	@Autowired
	private NewChannelMapper newChannelMapper;
	@Autowired
	private JcChannelExtMapper jcChannelExtMapper;

	// 初始化 d-channel 数据
	@GetMapping("/initDatas")
	public String initDatas() {
		List<NewChannel> list = newChannelMapper.selectList(null);
		for (NewChannel newChannel : list) {
			DChannel dChannel = new DChannel();
			dChannel.setChannelid(newChannel.getChannelid());
			dChannel.setChnlname(newChannel.getChnlname());
			dChannel.setParentid(newChannel.getParentid());
			dChannelMapper.insert(dChannel);
		}

		return "ok:" + LocalDateTime.now();
	}

	// 分析频道数据
	@GetMapping("/channel_sys")
	public String channel_sys() {
		List<DChannel> list = dChannelMapper.selectList(null);
		for (DChannel dChannel : list) {
			String chnlname = dChannel.getChnlname();
			QueryWrapper<JcChannelExt> qw = new QueryWrapper<>();
			qw.eq("channel_name", chnlname);
			List<JcChannelExt> dbChannelList = jcChannelExtMapper.selectList(qw);
			if (dbChannelList.size() == 0) {
				// System.err.println(dChannel.getChannelid() + ":" + chnlname + "----找不到分类");
			} else if (dbChannelList.size() == 1) {
				System.err.println(dChannel.getChannelid() + ":" + chnlname + "----可以匹配");
			} else {
				// System.err.println(dChannel.getChannelid() + ":" + chnlname + "----找到多个分类:" +
				// dbChannelList.size());
			}

		}

		return "ok:" + LocalDateTime.now();
	}

	// 查询不匹配分类,类似的分类,做参考依据
	@GetMapping("/channel_like")
	public String channel_like() {
		List<DChannel> list = dChannelMapper.selectList(null);
		int cnt = 0;
		for (DChannel dChannel : list) {
			String chnlname = dChannel.getChnlname();
			QueryWrapper<JcChannelExt> qw = new QueryWrapper<>();
			qw.eq("channel_name", chnlname);
			List<JcChannelExt> dbChannelList = jcChannelExtMapper.selectList(qw);
			if (dbChannelList.size() == 0) {
				cnt++;
				System.err.println(dChannel.getChannelid() + ":" + chnlname + "----找不到分类;找到类似分类如下:");
				QueryWrapper<JcChannelExt> qwLike = new QueryWrapper<>();
				qwLike.likeRight("channel_name", chnlname.substring(0, 2));
				List<JcChannelExt> dbChannelListLike = jcChannelExtMapper.selectList(qwLike);
				dbChannelListLike.forEach(e -> {
					System.err.println(e.getChannelName() + "\t\t\t" + e.getChannelId());
				});
				System.err.println("--------------------");
			} else if (dbChannelList.size() == 1) {
				// System.err.println(dChannel.getChannelid() + ":" + chnlname + "----可以匹配");
			} else {
				// System.err.println(dChannel.getChannelid() + ":" + chnlname + "----找到多个分类:" +
				// dbChannelList.size());
			}

		}

		System.err.println("总数:" + cnt);

		return "ok:" + LocalDateTime.now();
	}

	// 分析频道总数 : 拓尔思总数:423 , 客户类目总数:378
	@GetMapping("/channel_cnt")
	public String channel_cnt() {
		List<NewChannel> list = newChannelMapper.selectList(null); // 查询拓尔思总数
		List<JcChannelExt> listDb = jcChannelExtMapper.selectList(null); // 查询客户数据库总数

		return String.format("拓尔思总数:%s , 客户类目总数:%s ", list.size(), listDb.size());
	}
}
